package net.minecraft.src;

public class InscriptionThread implements Runnable {

	public Thread thread;
	public String inscription;
	public World world;
	public boolean[][] pattern;
	public boolean shouldExplode,
				   playSound;
	public int size,
			   posX,
			   count,
			   posY,
			   posZ,
			   blockID,
			   offsetX,
			   offsetY,
			   offsetZ;
	
	public InscriptionThread setShouldExplode(boolean shouldExplode) {
		this.shouldExplode = shouldExplode;
		return this;
	}
	public InscriptionThread setPlaySound(boolean playSound) {
		this.playSound = playSound;
		return this;
	}
	public InscriptionThread setOffsetX(int offsetX) {
		this.offsetX = offsetX;
		return this;
	}
	public InscriptionThread setOffsetY(int offsetY) {
		this.offsetY = offsetY;
		return this;
	}
	public InscriptionThread setOffsetZ(int offsetZ) {
		this.offsetZ = offsetZ;
		return this;
	}
	public String command;
	
	public InscriptionThread(String threadName) {
		thread = new Thread(this, threadName);
		thread.start();
	}
	public InscriptionThread setInscription(String s) {
		inscription = s;
		return this;
	}
	public InscriptionThread setThread(Thread thread) {
		this.thread = thread;
		return this;
	}
	public InscriptionThread setWorld(World world) {
		this.world = world;
		return this;
	}
	public InscriptionThread setPattern(boolean[][] pattern) {
		this.pattern = pattern;
		return this;
	}
	public InscriptionThread setSize(int size) {
		this.size = size;
		return this;
	}
	public InscriptionThread setPosX(int posX) {
		this.posX = posX;
		return this;
	}
	public InscriptionThread setCount(int count) {
		this.count = count;
		return this;
	}
	public InscriptionThread setBlockID(int blockID) {
		this.blockID = blockID;
		return this;
	}
	public InscriptionThread setPosY(int posY) {
		this.posY = posY;
		return this;
	}
	public InscriptionThread setPosZ(int posZ) {
		this.posZ = posZ;
		return this;
	}
	public InscriptionThread setCommand(String command) {
		this.command = command;
		return this;
	}
	@Override
	public void run() {
		System.out.println("Running! " + Thread.currentThread());
		if (command.startsWith("tunnel")) {

			mod_Inscription.debug("Tunnelling north.");
			for(int countLength = 0; countLength < 32; countLength++){
				for (int countHeight = 0; countHeight < 3; countHeight++) {
					for (int countWidth = 0; countWidth < 3; countWidth++) {
						if (command.contains("North")) {
							offsetX = -2;
							offsetY = 0;
							offsetZ = -1;
							placeBlockAndRest(posX+offsetX-countLength, posY+offsetY+countHeight, posZ+offsetZ+countWidth, blockID);
						}
						else if (command.contains("South")) {
							offsetX = 2;
							offsetY = 0;
							offsetZ = 1;
							placeBlockAndRest(posX+offsetX+countLength, posY+offsetY+countHeight, posZ+offsetZ-countWidth, blockID);
						}
						else if (command.contains("East")) {
							offsetX = -1;
							offsetY = 0;
							offsetZ = -2;
							placeBlockAndRest(posX+offsetX+countWidth, posY+offsetY+countHeight, posZ+offsetZ-countLength, blockID);
						}
						else if (command.contains("West")) {
							offsetX = 1;
							offsetY = 0;
							offsetZ = 2;
							placeBlockAndRest(posX+offsetX-countWidth, posY+offsetY+countHeight, posZ+offsetZ+countLength, blockID);
						}
					}
				}
			}
		}
//			Inscription.buildFromPattern(world, pattern, 0, size, posX - count, posY, posZ, command);
//			Inscription.buildFromPattern(world, pattern, 0, size, posX - count, posY + 1, posZ, command);
//			Inscription.buildFromPattern(world, pattern, 0, size, posX - count, posY + 2, posZ, command);
			
			
//			if (Thread.currentThread().equals(this)) {
//				try {
//					System.out.println("Resting..");
//					Thread.currentThread().sleep(1000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
		// TODO Auto-generated method stub

	}
	
	public void placeBlockAndRest(int x, int y, int z, int blockID) {
		placeBlockAndRest(x,y,z,blockID,100);
	}
	
	public void placeBlockAndRest(int x, int y, int z, int blockID, int time) {
		int blockID2;
		if (blockID == world.getBlockId(x, y, z)) {
			return;
		}
		blockID2 = world.getBlockId(x, y, z);
		world.setBlockWithNotify(x, y, z, blockID);
		if (playSound) {
			world.playSoundEffect(x+0.5d, y+0.5d, z+0.5d,
					Block.blocksList[blockID2].stepSound.func_1145_d(), (Block.blocksList[blockID2].stepSound.getVolume() + 1.0F) / 2.0F,
					Block.blocksList[blockID2].stepSound.getPitch() * 0.8F);
		}
//		if (shouldExplode) {
//			world.spawnParticle("explosion", x+0.5d, y+0.5d, z+0.5d, 1.0F, 1.0F, 1.0F);
//		}
		try {
			System.out.println("Resting..");
			Thread.currentThread().sleep(time);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
